package com.ifinc.common.socket.client;

import com.alibaba.fastjson.JSONObject;
import com.ifinc.common.socket.model.BaseResponse;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: luotao
 * Date: 12-5-30
 * Time: 19:06
 */

public class ObjectParserHandler {

    private Map<String, BaseResponse> resultMap = new HashMap<String, BaseResponse>();

    public void addResult(String key, BaseResponse value) {
        resultMap.put(key, value);
    }

    public BaseResponse getResponseObject(String key){
        return resultMap.get(key);
    }

    public BaseResponse removeResult(String key) {
        return resultMap.remove(key);
    }

    public void doReceived(String src) {

        String uuid = src.substring(0, 36);
        String str_json = src.substring(36);
        BaseResponse respTrade = resultMap.get(uuid);
        if (respTrade != null) {
            BaseResponse temp = JSONObject.parseObject(str_json, respTrade.getClass());
            respTrade.setResultResp(temp);
            respTrade.release();
        } else {
            System.out.println("not found");
        }
    }
}
